<?php

defined('BY_JYA') or exit('error');
/*
	重复购
*/
$tabs=array(
	array('op'=>'list','title'=>'礼品列表'),
	array('op'=>'edit','title'=>'添加/编辑礼品'),
);
set_html('tabs',$tabs);
$_RQ['op']=$_RQ['op']?:'list';
$return['libao_types']=$this->libao_types;
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['params']=array();
	if($_RQ['type']){
		$return['params']['type']=$_RQ['type'];
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array(intval($_RQ['page'])?:1,10),
		'orderby'=>'id desc'
	);
	$list=$this->get_list('libao',$parameter,$total);
	if($list){
		foreach($list as &$item){
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：设置不同的礼品，然后创建活动礼包，添加礼品到礼包！！','list'=>array(
			array('name'=>'title','title'=>'礼品标题','type'=>'string'),
			array('name'=>'logo','title'=>'礼品图片','type'=>'image'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'libao_types'),
			array('name'=>'price','title'=>'价值','type'=>'string'),
			array('name'=>'createtime','title'=>'活动创建时间','type'=>'time'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>15,'name'=>'title','title'=>'礼品标题'),
			array('type'=>'select','width'=>15,'name'=>'type','title'=>'礼品类型','options'=>'libao_types'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
	
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if($post['type']==2 && !is_numeric($post['zhe_fee'])){
			exi('折扣必须是整数或小数','error');
		}
		if($post['type']==2 && ($post['zhe_fee']<0 || $post['zhe_fee']>10)){
			exi('折扣不能小于0/大于10','error');
		}
		$data=array(
			'title'=>$post['title'],
			'logo'=>$post['logo'],
			'price'=>$post['price'],
			'goods_id'=>intval($post['goods_id']),
			'coupon_id'=>intval($post['coupon_id']),
			'member_levelid'=>intval($post['member_levelid']),
			'price'=>$post['price'],
			'zhe_fee'=>$post['zhe_fee'],
			'del_fee'=>$post['del_fee'],
			'del_goods_ids'=>ijson_encode($post['del_goods_ids']),
			'zhe_goods_ids'=>ijson_encode($post['zhe_goods_ids']),
			'type'=>intval($post['type']),
		);
		$id=intval($post['id']);
		if($id){
			$this->update('libao',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			$this->insert('libao',$data);
			$id=$this->insertid();
		}
		exi('设置成功！','','list');
	}
	$id = intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_giftbao_libao',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
		$item['type'] = '0';
	}
	$item['del_goods_ids']=$item['del_goods_ids']?json_decode($item['del_goods_ids'],true):array();
	$item['zhe_goods_ids']=$item['zhe_goods_ids']?json_decode($item['zhe_goods_ids'],true):array();
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置礼品详情',
		'post'=>false,
		'edit'=>array(
			array('name'=>'title','title'=>'礼品标题','type'=>'string'),
			array('name'=>'logo','title'=>'礼品LOGO','type'=>'image'),
			array('name'=>'price','title'=>'礼品价值','type'=>'unitstring','unit'=>'元'),
			array('name'=>'type','title'=>'礼品类型','type'=>'radio','options'=>'libao_types'),
			array('type'=>'choose','action'=>'jy_weishop/coupon.get_coupon/','name'=>'coupon_id','title'=>'赠送优惠券ID','show'=>array('type'=>0)),
			array('type'=>'choose','action'=>'jy_weishop/goods.get_goods/','name'=>'goods_id','title'=>'赠送商品ID','show'=>array('type'=>3)),
			array('type'=>'select','options'=>'levels','name'=>'member_levelid','title'=>'赠送会员等级','show'=>array('type'=>4)),
			array('name'=>'del_fee','title'=>'减免金额','type'=>'unitstring','unit'=>'元','show'=>array('type'=>1),'help'=>''),
			array('name'=>'del_goods_ids','title'=>'参与减免的商品','type'=>'choose_many','action'=>'jy_weishop/goods.get_goods/many/','choose_title'=>'选择参与的商品','show'=>array('type'=>1),'help'=>'不填则适合所有商品！'),
			array('name'=>'zhe_fee','title'=>'折扣权益%','type'=>'unitstring','unit'=>'%','show'=>array('type'=>2),'help'=>'例如：填写8.5表示打85折'),
			array('name'=>'zhe_goods_ids','title'=>'参与折扣的商品','type'=>'choose_many','action'=>'jy_weishop/goods.get_goods/many/','choose_title'=>'选择参与的商品','show'=>array('type'=>2),'help'=>'不填则适合所有商品！'),
		),
	);
	$return['levels']=pdo_getall('jy_weishop_member_level',array('uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_giftbao_libao',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}